package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentTechnicalMapper;
import com.zhijian.medical.entity.po.AssessmentTechnical;
import com.zhijian.medical.entity.vo.request.AssessmentTechnicalSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.AssessmentTechnicalSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentTechnicalVo;
import com.zhijian.medical.service.AssessmentTechnicalService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人员能力考核-技术能力考核业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 13:57:59
 */
@Slf4j
@Service
public class AssessmentTechnicalServiceImpl implements AssessmentTechnicalService {

	@Autowired
    private AssessmentTechnicalMapper mapper;

    @Override
    public AssessmentTechnical selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentTechnicalVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(AssessmentTechnical assessmentTechnical) {
        Date now = new Date();
        assessmentTechnical.setCreateTime(now);
        assessmentTechnical.setUpdateTime(now);
        return mapper.insert(assessmentTechnical) > 0 ? assessmentTechnical.getId() : 0L;
    }

    @Override
    public Long insertSelective(AssessmentTechnical assessmentTechnical) {
        Date now = new Date();
        assessmentTechnical.setCreateTime(now);
        assessmentTechnical.setUpdateTime(now);
        return mapper.insertSelective(assessmentTechnical) > 0 ? assessmentTechnical.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentTechnical assessmentTechnical) {
        Date now = new Date();
        assessmentTechnical.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentTechnical) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentTechnical record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AssessmentTechnicalSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechnical assessmentTechnical = new AssessmentTechnical();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechnical);
        return this.insert(assessmentTechnical) != 0;
    }

    @Override
    public boolean modify(AssessmentTechnicalSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechnical old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        AssessmentTechnical assessmentTechnical = new AssessmentTechnical();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechnical);
        return this.update(assessmentTechnical);
    }

    @Override
    public AssessmentTechnicalVo selectOne(AssessmentTechnicalSearchVo assessmentTechnicalSearchVo) {
        return mapper.selectOne(assessmentTechnicalSearchVo);
     }

    @Override
    public Integer count(AssessmentTechnicalSearchVo assessmentTechnicalSearchVo) {
        return mapper.count(assessmentTechnicalSearchVo);
    }

    @Override
    public List<AssessmentTechnicalVo> selectList(AssessmentTechnicalSearchVo assessmentTechnicalSearchVo) {
        return mapper.selectList(assessmentTechnicalSearchVo);
    }

    @Override
    public Page<AssessmentTechnicalVo> selectPage(AssessmentTechnicalSearchVo assessmentTechnicalSearchVo) {
        AssertUtil.check(Objects.isNull(assessmentTechnicalSearchVo.getPageNum()) || Objects.isNull(assessmentTechnicalSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(assessmentTechnicalSearchVo);
        //分页
        Page<AssessmentTechnicalVo> page = new Page<>(true, count, assessmentTechnicalSearchVo.getPageNum(), assessmentTechnicalSearchVo.getPageSize());
        assessmentTechnicalSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentTechnicalVo> responseVos = this.selectList(assessmentTechnicalSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentTechnical> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AssessmentTechnicalSearchVo assessmentTechnicalSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(assessmentTechnicalSearchVo);
    }
    
}